package com.funong.matrix.biz.base.dao;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.funong.matrix.biz.base.entity.SysUserEntity;
import com.funong.matrix.biz.base.mapper.SysUserMapper;
import com.funong.matrix.biz.user.pojo.dto.UserDTO;
import com.funong.matrix.core.base.Dao;
import com.funong.matrix.core.base.FunongBaseDao;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.enums.IsDeletedEnum;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

/**
 * @author wanjialin
 * @version 1.0
 * @date 2023/5/18
 */
@Dao
@Aspect
public class SysUserDao extends FunongBaseDao<SysUserMapper, SysUserEntity> {


    public IPage<SysUserEntity> queryUser(QueryDTO<UserDTO> queryDTO) {
        ObjectMapper mapper = new ObjectMapper();
        UserDTO userDTO = mapper.convertValue(queryDTO.getCondition(), UserDTO.class);

        return this.page(new Page<>(queryDTO.getPageNo(), queryDTO.getPageSize()),
                Wrappers.<SysUserEntity>lambdaQuery()
                        .like(StringUtils.isNotEmpty(userDTO.getName()), SysUserEntity::getName, userDTO.getName())
                        .like(StringUtils.isNotEmpty(userDTO.getPhone()), SysUserEntity::getPhone, userDTO.getPhone())
                        .eq(null != userDTO.getStatus(), SysUserEntity::getStatus, userDTO.getStatus())
                        .eq(StringUtils.isNotEmpty(userDTO.getInitialCompanyCode()), SysUserEntity::getInitialCompanyCode, userDTO.getInitialCompanyCode())
                        .eq(StringUtils.isNotEmpty(userDTO.getDeptCode()), SysUserEntity::getDeptCode, userDTO.getDeptCode())
                        .eq(StringUtils.isNotEmpty(userDTO.getCompanyCode()), SysUserEntity::getCompanyCode, userDTO.getCompanyCode())
                        .eq(SysUserEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
                        .eq(null != userDTO.getCustomerId(), SysUserEntity::getCustomerId, userDTO.getCustomerId())
        );
    }

    public List<SysUserEntity> userLogin(SysUserEntity sysUserEntity) {
        return this.list(
                Wrappers.<SysUserEntity>lambdaQuery()
                        .eq(SysUserEntity::getAccount, sysUserEntity.getAccount())
                        .eq(SysUserEntity::getPassword, sysUserEntity.getPassword())
                        .eq(SysUserEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
        );
    }

    public List<SysUserEntity> queryUserByAccount(String account, Integer systemId) {
        return this.list(
                Wrappers.<SysUserEntity>lambdaQuery()
                        .eq(SysUserEntity::getAccount, account)
                        .eq(systemId != null, SysUserEntity::getSystemId, systemId)
                        .eq(SysUserEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
        );
    }

    public SysUserEntity queryUserDesc() {
        return this.getOne(
                Wrappers.<SysUserEntity>lambdaQuery()
                        .like(SysUserEntity::getAccount,"100000%")
                        .orderByDesc(SysUserEntity::getId)
                        .last("limit 1")
        );
    }

    /**
     * 根据权限编码获取对应的用户列表数据信息
     * @param permissionCode
     * @return
     */
    public List<SysUserEntity> queryUserByPermission(String permissionCode) {
        return this.baseMapper.queryUserByPermission(permissionCode);
    }
}
